import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import(/* webpackChunkName: "about" */ '../views/home.vue'),
    // 中间内容区路由
    children: [
      {
        //用户列表
        path: 'users',
        name: 'users',
        component: () => import(/* webpackChunkName: "about" */ '../views/users.vue')
      },
      {
        //角色列表
        path: 'roles',
        name: 'roles',
        component: () => import(/* webpackChunkName: "about" */ '../views/roles.vue')
      },
      {
        //角色列表
        path: 'rights',
        name: 'rights',
        component: () => import(/* webpackChunkName: "about" */ '../views/rights.vue')
      },
      {
        //商品
        path: 'goods',
        name: 'goods',
        component: () => import(/* webpackChunkName: "about" */ '../views/goods.vue')
      },
      {
        //商品分类列表
        path: 'categories',
        name: 'categories',
        component: () => import(/* webpackChunkName: "about" */ '../views/categories.vue')
      },
      {
        //商品分类列表
        path: 'params',
        name: 'params',
        component: () => import(/* webpackChunkName: "about" */ '../views/params.vue')
      },
      {
        //商品分类列表
        path: 'orders',
        name: 'orders',
        component: () => import(/* webpackChunkName: "about" */ '../views/orders.vue')
      },
      {
        //数据分析
        path: 'reports',
        name: 'reports',
        component: () => import(/* webpackChunkName: "about" */ '../views/reports.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "about" */ '../views/login.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import(/* webpackChunkName: "about" */ '../views/home.vue')
  }
]

const router = new VueRouter({
  routes
})

//导航守卫 防止未登录进入home
router.beforeEach((to, from, next) => {
  if (to.path == '/' || to.path == '/home') {
    if (!localStorage.getItem('userinfo')) {
      next('/login')
    }
  }
  //如果已经登陆了，从哪里来回哪里去。登录页面只能在未登录的情况下才能访问
  if (to.path == '/login') {
    if (localStorage.getItem('userinfo')) {
      next(from)
    }
  }

  next()
})
export default router
